import { FC, useState } from 'react';
import { Card, Form, Input, Button, Space, Checkbox } from 'antd';
import { UserOutlined, LockOutlined } from '@ant-design/icons';

// const layout = {
//   labelCol: { span: 5 },
//   wrapperCol: { span: 19 },
// };

const Index: FC = () => {
  // Form组件下的useForm的hooks，生成一个form实例
  const [form] = Form.useForm();
  const [form2] = Form.useForm();
  const [username, set_username] = useState('zhangsan');

  const onFinish = (values: any) => {
    console.log('Finish:', values);
  };

  const handleClick = () => {
    // console.log(form);
    // getFieldsValue这个方法去获取表单的所有属性
    // console.log(form.getFieldsValue());
    // getFieldValue获取单个属性
    // console.log(form.getFieldValue('username'));

    const arr = form.getFieldsError().filter((item) => item.errors.length);
    console.log(arr);
    if (!arr.length) {
      console.log(form.getFieldsValue());
    }
  };

  const handleClick2 = () => {
    // 重置表单
    // form.resetFields();
    // 判断全部的表单元素都操作过了
    // console.log(form.isFieldsTouched(true));
    // 设置表单里面的值
    // form.setFieldsValue({ username: 'Hi, man!' });

    // set_username('lisi');
    form.setFieldsValue({ username: 'lisi' });
  };

  return (
    <Space direction="vertical" style={{ display: 'flex' }}>
      <Card title="登录行内表单">
        <Form
          form={form}
          name="horizontal_login"
          // 表单是横着的
          layout="inline"
          // "submit"的按钮点击时触发的
          onFinish={onFinish}
          // 设置表单初始值
          initialValues={{
            username: username,
          }}
        >
          {/* 表单里面的每一项都要用Form.Item套住 */}
          <Form.Item
            // label=""
            // Item的name属性关系到里面的表单元素要绑定对象的那一个变量
            name="username"
            // 验证规则
            // 每一个对象都是一个验证的规则，每个对象都可以有message
            rules={[
              { required: true, message: '用户名不能为空' },
              // { max: 6, message: '最大长度不能超过6位' },
              // () => ({
              //   validator(_, value) {
              //     if (value === 'zhangsan') {
              //       return Promise.resolve();
              //     }
              //     return Promise.reject(new Error('你输入的只能时张三'));
              //   },
              // }),
              // {
              //   pattern: /^([a-zA-Z\d])(\w|\-)+@[a-zA-Z\d]+\.[a-zA-Z]{2,4}$/,
              //   message: '请输入正确的手机号',
              // },
            ]}
          >
            <Input
              prefix={<UserOutlined className="site-form-item-icon" />}
              placeholder="Username"
            />
          </Form.Item>
          <Form.Item
            name="password"
            rules={[{ required: true, message: 'Please input your password!' }]}
          >
            <Input
              prefix={<LockOutlined className="site-form-item-icon" />}
              type="password"
              placeholder="Password"
            />
          </Form.Item>
          {/* <Form.Item>
            <Button type="primary" htmlType="submit">
              Log in
            </Button>
          </Form.Item> */}
        </Form>

        <Button type="primary" onClick={handleClick}>
          登录
        </Button>
        <Button type="primary" onClick={handleClick2}>
          重置
        </Button>
      </Card>

      <Card title="登录竖向表单">
        <Form
          // {...layout}
          // 定义label的宽度
          labelCol={{ span: 5 }}
          wrapperCol={{ span: 19 }}
          form={form2}
          name="normal_login"
          className="login-form"
          initialValues={{ remember: true }}
          onFinish={onFinish}
        >
          <Form.Item
            label="用户名"
            name="username"
            rules={[{ required: true, message: 'Please input your Username!' }]}
          >
            <Input
              prefix={<UserOutlined className="site-form-item-icon" />}
              placeholder="Username"
            />
          </Form.Item>
          <Form.Item
            label="密码"
            name="password"
            rules={[{ required: true, message: 'Please input your Password!' }]}
          >
            <Input
              prefix={<LockOutlined className="site-form-item-icon" />}
              type="password"
              placeholder="Password"
            />
          </Form.Item>
          <Form.Item>
            <Form.Item name="remember" valuePropName="checked" noStyle>
              <Checkbox>Remember me</Checkbox>
            </Form.Item>

            <a className="login-form-forgot" href="">
              Forgot password
            </a>
          </Form.Item>

          <Form.Item>
            <Button
              type="primary"
              htmlType="submit"
              className="login-form-button"
            >
              Log in
            </Button>
            Or <a href="">register now!</a>
          </Form.Item>
        </Form>
      </Card>
    </Space>
  );
};

export default Index;
